# freeband.py
# Copyright (C) 2007 Sergio Perez Camacho - BRUE
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA



import sys, os, os.path
import soya, soya.sdlconst

import basic3d
import basicsfx

MAX_OBJ_CACHE = 20


if __name__=="__main__":
    

    # Initializes soya (3D engine)
    soya.init("FreeBand", 640,480,0)
    soya.path.append(os.path.join(os.path.dirname(sys.argv[0]), "data"))
    
    # Creates the scene
    scene = soya.World()
    


    light = soya.Light(scene)
    light.set_xyz(0.5, 2.0, 2.0)

    # Creates a camera.

    camera = soya.Camera(scene)
    camera.z = 7
    camera.y = 9
    camera.rotate_x(-40)
    soya.set_root_widget(camera)

    basicsfx.init()
    s = basicsfx.BaseSong(sys.argv[1])

    rBeats = []

    c = 0
    le = MAX_OBJ_CACHE
    if len(s.beats[0]) < MAX_OBJ_CACHE: le = len(s.beats[0])
    while (( c < MAX_OBJ_CACHE) & (c < len(s.beats[0]))):
        rBeats.append(basic3d.BasicElement("BasicDrum1", scene, -6, -4, -s.beats[0][c], s,le,0))
        c = c + 1
    c = 0
    le = MAX_OBJ_CACHE
    if len(s.beats[1]) < MAX_OBJ_CACHE: le = len(s.beats[1])
    while (( c < MAX_OBJ_CACHE) & (c < len(s.beats[1]))):
        rBeats.append(basic3d.BasicElement("BasicDrum2", scene, -3, -4, -s.beats[1][c], s,le,1))
        c = c + 1
        
    c = 0
    le = MAX_OBJ_CACHE
    if len(s.beats[2]) < MAX_OBJ_CACHE: le = len(s.beats[2])
    while (( c < MAX_OBJ_CACHE) & (c < len(s.beats[2]))):
        rBeats.append(basic3d.BasicElement("BasicDrum1", scene, -0, -4, -s.beats[2][c], s,le,2))
        c = c + 1
    c = 0
    le = MAX_OBJ_CACHE
    if len(s.beats[3]) < MAX_OBJ_CACHE: le = len(s.beats[3])
    while (( c < MAX_OBJ_CACHE) & (c < len(s.beats[3]))):
        rBeats.append(basic3d.BasicElement("BasicDrum1", scene,  3, -4, -s.beats[3][c], s,le,3))
        c = c + 1
    c = 0
    le = MAX_OBJ_CACHE
    if len(s.beats[4]) < MAX_OBJ_CACHE: le = len(s.beats[4])
    while (( c < MAX_OBJ_CACHE) & (c < len(s.beats[4]))):
        rBeats.append(basic3d.BasicElement("BasicDrum1", scene,  6, -4, -s.beats[4][c], s,le,4))
        c = c + 1

 
    
    p = basic3d.Plane("DrumPlane", scene, 0,-5,-7.5, rBeats)

    s.play()
    

    soya.MainLoop(scene).main_loop()
    
